<template>
  <!-- 编辑器预览组件 -->
  <el-dialog :visible.sync="dialogVisible" :show-close="false" width="800px" @close="close">
    <!-- <div style="text-align: right;">
      <i class="el-icon-close" style="color: #fff;font-size: 24px;margin-right: 20px;margin-bottom: 10px;" @click="close"></i>
    </div> -->
    <div class="drawer-wrap">
      <div class="header">
          <div class="search">
            <img class="search-icon" src="@/assets/icons/search.png" alt="">
            <el-input v-model="queryParams.queryContent" placeholder="搜索"></el-input>
          </div>
          <div>
            <el-button type="success" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            <el-button type="primary" icon="el-icon-check" @click="handleQuery">搜索</el-button>
          </div>
      </div>
      <!-- 列表 -->
      <el-tabs v-model="activeName" @tab-click="changeTab">
        <el-tab-pane label="• 个人" name="private"></el-tab-pane>
        <el-tab-pane label="• 公共" name="public"></el-tab-pane>
      </el-tabs>
      <div class="material-list" v-loading="loading">
        <div class="material-item" v-for="(item, index) in retrieveList" :key="index">
          <div class="material-item-content">
            <div class="material-title">
              <div class="title">{{ item.name }}</div>
              <div style="margin-top: 2px;">
                <el-checkbox v-model="item.checked"></el-checkbox>
              </div>
            </div>
            <!-- 视频 -->
            <div class="material-video">
              <!-- <video :src="item.previewUrl"></video> -->
              <video controls="" name="media" class="frame-img" :poster="item.thumbnail">
                <source :src="item.previewUrl" type="video/mp4">
              </video>
            </div>
            <div class="material-tag">
              <span class="tag text" v-for="(cell, index) in item.labels" :key="index">{{ cell }}</span>
            </div>
            <div class="material-time">上传时间：{{ parseTime(new Date(item.publishDatetime), '{y}-{m}-{d}') }}</div>
            <!-- <div class="material-btn">
              <el-button class="btn" size="mini" @click="handlePreview(item)">预览</el-button>
              <el-button type="primary" size="mini" class="primary-btn" @click="handleInsertion(item)">插入</el-button>
            </div> -->
          </div>
        </div>
        <div style="text-align: center;flex: 1;color: #888;" class="no-data" v-if="retrieveList.length===0 && !loading">
          暂无数据
        </div>
      </div>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getVideoList" />

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button class="info-button" type="info" @click="close" icon="el-icon-close">取 消</el-button>
      <el-button class="primary-btn" type="primary" @click="handleInsertion" icon="el-icon-circle-plus-outline">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getVideoClipSearch } from "@/api/material/material";

export default {
  name: "Preview",
  data() {
    return {
      title: "",
      loading: true,
      dialogVisible: false,
      retrieveList: [],
      activeName: 'private',
      kerWord: '',
      total:0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        queryContent:''
      },
      checked:false,
      selectList:[]
    };
  },
  computed:{
    userId() {
      return this.$store.getters.userId;
    },
  },
  watch: { 
    retrieveList: {
      handler(val) {
        const list = this.retrieveList.filter(res=>res.checked)
        if(list.length>0) {
          this.selectList = list
        }
      },
      deep: true
    }
  },
  mounted() { },
  methods: {
    close() {
      this.dialogVisible = false;
    },
    show(taskVideo,queryContent) {
      this.dialogVisible = true;
      this.queryParams.userId = this.userId
      this.queryParams.taskId = taskVideo.taskId
      this.queryParams.queryContent = queryContent
      this.retrieveList = []
      this.total = 0
      this.getVideoList()
    },
    // 获取视频素材
    getVideoList() {
      this.loading = true;
      getVideoClipSearch(this.queryParams).then(response => {
        console.log(response,'54444444444')
        this.retrieveList = response.rows.map(res=>{
          res.checked = false
          return res
        })
        // if(this.selectList.length>0){
        //   for (const j in this.selectList) {
        //     for (const k in this.retrieveList) {
        //       if (this.selectList[j].id === this.retrieveList[k].id) {
        //         this.retrieveList[k].checked = true
        //       }else{
        //         this.retrieveList[k].checked = false
        //       }
        //     }
        //   }
        // }
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      })
    },
    // 切换个人公共
    changeTab() {
      if (this.activeName === 'private') {
        this.queryParams.userId = this.userId
      } else {
        this.queryParams.userId = ''
      }
      this.retrieveList = []
      this.getVideoList()
    },
    // 预览
    handlePreview() {

    },
    // 插入
    handleInsertion() {
      this.$emit('insertion', this.selectList)
      this.close()
    },
    // 重置搜索
    resetQuery() {
      // this.resetForm("queryForm");
      this.handleQuery();
    },
     /** 搜索按钮操作 */
     handleQuery() {
      this.queryParams.pageNum = 1;
      this.retrieveList = []
      this.total = 0
      this.getVideoList();
    },
  },
};
</script>
<style scoped lang="scss">
.app-container {
  position: relative;
}
.drawer-wrap{
  background-color: #fff;
  border-radius: 12px;
  padding-top: 20px;
}
.header {
  display: flex;
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 10px;
  justify-content: space-between;
  .search{
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #F7F8FA;
    border-radius: 4px;
    margin-right: 20px;
    flex: 1;
  }
  .search-icon{
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
  .line {
    display: inline-block;
    width: 2px;
    height: 13px;
    background-color: #2552E4;
    margin-right: 5px;
  }

  i {
    font-size: 26px;
    cursor: pointer;
    margin-left: 60px;
  }
}

.material-list {
  min-height: 200px;
  max-height: 550px;
  overflow: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .material-item {
    margin-bottom: 12px;
    width: 32%;
    .material-item-content {
      background: #F7F8FA;
      padding: 10px;
      border-radius: 4px;
    }

    .material-title {
      display: flex;
      margin-bottom: 2px;
      /*这里是超出几行省略*/
      .title{
        flex: 1;
        height: 40px;
        font-size: 14px;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        margin-right: 10px;

      }
    }

    .material-video {
      img {
        width: 100%;
        height: 140px;
      }

      video {
        width: 100%;
        height: 140px;
      }
    }

    .material-desc {
      font-size: 12px;
      color: rgba(255, 255, 255, .8);
      line-height: 20px;
      word-break: break-all;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      /*这里是超出几行省略*/
      overflow: hidden;
    }

    .material-tag {
      margin: 10px 0 10px;

      .tag {
        max-width: 100px;
        display: inline-block;
        background: rgba(9, 184, 68, 0.08);
        color: #09B844;
        padding: 3px 10px;
        font-size: 12px;
        border-radius: 2px;
        margin-right: 10px;
      }
      .text {
        overflow: hidden; /* 隐藏超出的内容 */
        text-overflow: ellipsis; /* 使用省略号表示被隐藏的文本 */
        white-space: nowrap; /* 不允许文本换行 */
      }
      .tag:last-child {
        background: rgba(0, 87, 255, 0.08);
        color: #0057FF;
      }
    }

    .material-time {
      font-size: 12px;
    }

    .material-btn {
      display: flex;
      margin-top: 15px;
      .btn{
        border: none;
        background: transparent;
      }
      .btn:hover{
        background: none;
      }
      .primary-btn {
        padding: 7px 30px;
      }
    }
  }
}

::v-deep {
  .el-dialog__header {
    padding: 0;
  }

  .el-drawer {
    background: transparent;
  }
  .el-input__inner{
    background: transparent !important;
    border: none;
  }
  .el-tabs__nav-wrap::after{
    background: transparent;
  }
}
</style>
